window.onload = function(){
    var index = parent.layer.getFrameIndex(window.name);
    var url = window.location.search;//你的地址

    if (url.indexOf("?") != -1) {
        var str = url.substr(1)
        strs = str.split("&");
        var page = strs[0].split("=")[1];
    }
    console.log(page);
    $.ajax({
        url:constUrl+"CategoryServlet",
        type:'post',
        data:{
            operateType:"showDetail",
            categoryId:page
        },
        dataType:'json',
        success: function(responseText) {
            console.log(responseText);
            $("#name").val(responseText.datas[0].categoryName);
            $("#classification").val(responseText.datas[0].isFunction);
        },
        complete: function() {
        },
        error: function() {
        }
    });

    $('#updataCategory').click(function () {
        var name = $("#name").val();
        var classification = $("#classification").val();
        var validators = new  InputValidators();

        validators.importStrategies(validationStrategies);

        validators.addValidationStrategy('isEqual',  function(element, errMsg, value1, value2) {
            if(value1 !== value2) {
                return this.buildInvalidObj(element, errMsg, value1 );
            }
        });
        var category_name = document.getElementById('name');

        validators.addValidator('isNoEmpty', category_name, '分类名称未填写，请填写完整', category_name.value);
        var vategory_classification = document.getElementById('classification');
        validators.addValidator('isNoEmpty', vategory_classification, '分类类型未选择，请返回选择', vategory_classification.value);
        var result = validators.check();
        if(result){
            layer.msg(result.errMsg);
            // alert(result.errMsg);
            result.element.focus();
            result.element.select();
            return false;
        }
        $.ajax({
            url:constUrl+"CategoryServlet",
            type:'post',
            data:{
                operateType:"alter",
                isFunction:classification,
                categoryName:name,
                categoryId:page

            },
            dataType:'json',
            success: function(responseText) {
                if(responseText.result) {
                    parent.layer.confirm('修改成功！',{
                        btn:['确定']
                    },function () {
                        parent.location.reload(index);
                        parent.layer.close(index);
                    })
                }
            },
            complete: function() {
            },
            error: function() {
            }
        });
    });
    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
}
//验证准备
//输入验证器
function InputValidators(){
    this.validators = [];
    this.strategies = {};
}

//添加验证方法
//参数:
//  rule: 验证策略字符串
//  element: 被验证的dom元素
//  errMsg: 验证失败时显示的提示信息
//  value: 被验证的值
InputValidators.prototype.addValidator = function(rule, element, errMsg, value) {
    var that = this;
    var ruleElements = rule.split(":");

    this.validators.push(function() {
        var strategy = ruleElements.shift();
        var params = ruleElements;
        params.unshift(value);
        params.unshift(errMsg);
        params.unshift(element);

        return that.strategies[strategy].apply(that, params);
    });
};

//添加验证策略函数
//参数:
//  name: 策略名称
//  strategy: 策略函数
InputValidators.prototype.addValidationStrategy = function(name, strategy){
    this.strategies[name] = strategy;
};

//从策略对象导入验证策略函数
//参数:
//  strategies: 包含各种策略函数的对象
InputValidators.prototype.importStrategies = function(strategies) {
    for(var strategyName in strategies) {
        this.addValidationStrategy(strategyName, strategies[strategyName]);
    }
};

//验证失败时，将相关的错误信息打包返回
//参数:
//  element: dom元素
//   errMsg: 验证失败时的提示消息
//    value: 被验证的值
InputValidators.prototype.buildInvalidObj = function(element, errMsg, value){
    return {
        'value': value,
        'element': element,
        'errMsg': errMsg
    };
};

//开始验证
InputValidators.prototype.check = function() {
    for(var i = 0, validator; validator = this.validators[i++];){
        var result = validator();
        if(result) {
            return result;
        }
    }
};

var validationStrategies = {
    isNoEmpty: function(element, errMsg, value) {
        if(value === '') {
            return this.buildInvalidObj(element, errMsg, value );
        }
    }
};